package cn.yuanqiao.archive.storeRoom.service.impl;

import cn.yuanqiao.archive.appManager.domain.DataScopeBase;
import cn.yuanqiao.archive.storeRoom.domain.SafetyCheck;
import cn.yuanqiao.archive.storeRoom.mapper.SafetyCheckMapper;
import cn.yuanqiao.archive.storeRoom.service.ISafetyCheckService;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * SafetyCheckService业务层处理
 * 
 * @author xy
 * @date 2023-06-15
 */
@Service
public class SafetyCheckServiceImpl implements ISafetyCheckService
{
    @Autowired
    private SafetyCheckMapper safetyCheckMapper;

    /**
     * 查询SafetyCheck
     * 
     * @param id SafetyCheck主键
     * @return SafetyCheck
     */
    @Override
    public SafetyCheck selectSafetyCheckByID(Long id)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)) {
            return safetyCheckMapper.selectSafetyCheckByID(id,"");
        }else{
            return safetyCheckMapper.selectSafetyCheckByID(id,tenantCode.toUpperCase());

        }
    }

    /**
     * 查询SafetyCheck列表
     * 
     * @param safetyCheck safetyCheck
     * @return SafetyCheck
     */
    @Override
    public List<SafetyCheck> selectSafetyCheckList(SafetyCheck safetyCheck)
    {
        //获取租户的code
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)){
            throw new ServiceException("TenantCode is null");
        }else{
            return safetyCheckMapper.selectSafetyCheckList(new DataScopeBase(),safetyCheck,tenantCode.toUpperCase());

        }

    }

    /**
     * 新增SafetyCheck
     * 
     * @param safetyCheck SafetyCheck
     * @return 结果
     */
    @Override
    public int insertSafetyCheck(SafetyCheck safetyCheck)
    {
/*
        safetyCheck.setID(DateUtils.getNowDate());
*/
        safetyCheck.setIsDel(0);
        safetyCheck.setId(SnowflakeIdGenerator.generateId());
        safetyCheck.setCreator(SecurityUtils.getUserId());
        safetyCheck.setCreateTime(DateUtils.getNowDate());
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)) {
            return safetyCheckMapper.insertSafetyCheck(safetyCheck,"");
        }else{
            return safetyCheckMapper.insertSafetyCheck(safetyCheck,tenantCode.toUpperCase());

        }
    }

    /**
     * 修改SafetyCheck
     * 
     * @param safetyCheck SafetyCheck
     * @return 结果
     */
    @Override
    public int updateSafetyCheck(SafetyCheck safetyCheck)
    {
        safetyCheck.setLastUpdater(SecurityUtils.getUserId());
        safetyCheck.setLastupdateTime(DateUtils.getNowDate());
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)) {
        return safetyCheckMapper.updateSafetyCheck(safetyCheck,"");
        }else {
            return safetyCheckMapper.updateSafetyCheck(safetyCheck,tenantCode.toUpperCase());

        }

    }

    /**
     * 批量删除SafetyCheck
     * 
     * @param ids 需要删除的SafetyCheck主键
     * @return 结果
     */
    @Override
    public int deleteSafetyCheckByIds(Long[] ids)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)) {
            return safetyCheckMapper.deleteSafetyCheckByIds(ids,"");
        }else{
            return safetyCheckMapper.deleteSafetyCheckByIds(ids,tenantCode.toUpperCase());

        }
    }

    /**
     * 删除SafetyCheck信息
     * 
     * @param id SafetyCheck主键
     * @return 结果
     */
    @Override
    public int deleteSafetyCheckByID(Long id)
    {
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)) {
            return safetyCheckMapper.deleteSafetyCheckById(id,"");
        }else{
            return safetyCheckMapper.deleteSafetyCheckById(id,tenantCode.toUpperCase());

        }
    }
}
